package com.cmes.mes.audit.service;

import com.cmes.jdbc.service.GenericService;
import com.cmes.mes.audit.constant.AuditOperationEnum;
import com.cmes.mes.audit.entity.AuditLogEntity;
import com.cmes.mes.audit.entity.AuditModule;
import com.cmes.mes.audit.repository.AuditLogRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 审核流程日志
 *
 * @author Bruce.Gong
 * @since 1.0.0-SNAPSHOT
 */
@Slf4j
@RequiredArgsConstructor
@Service
@Transactional(propagation = Propagation.SUPPORTS, rollbackFor = Exception.class)
public class AuditLogService extends GenericService<AuditLogRepository, AuditLogEntity> {

    /**
     * 开启流程
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void start(String businessCode, AuditModule entity) {
        insert(AuditLogEntity.builder()
                        .businessCode(businessCode)
                        .businessId(entity.getId())
                        .operation(AuditOperationEnum.START.getValue())
                        .status(AuditOperationEnum.START.getStatus())
                        .build());
    }

    /**
     * 批量开启流程
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void start(String businessCode, List<? extends AuditModule> entities) {
        insertBatch(entities.stream()
                .map(v -> AuditLogEntity.builder()
                        .businessCode(businessCode)
                        .businessId(v.getId())
                        .operation(AuditOperationEnum.START.getValue())
                        .status(AuditOperationEnum.START.getStatus())
                        .build())
                .collect(Collectors.toList()));
    }

    /**
     * 提交
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void submit(String businessCode, List<? extends AuditModule> entities, String cause) {
        log(businessCode, AuditOperationEnum.SUBMIT, entities, cause);
    }

    /**
     * 驳回
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void reject(String businessCode, List<? extends AuditModule> entities, String cause) {
        log(businessCode, AuditOperationEnum.REJECT, entities, cause);
    }

    /**
     * 审核通过
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void audit(String businessCode, List<? extends AuditModule> entities, String cause) {
        log(businessCode, AuditOperationEnum.AUDITED, entities, cause);
    }

    /**
     * 通过驳回
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void reAudit(String businessCode, List<? extends AuditModule> entities, String cause) {
        log(businessCode, AuditOperationEnum.RE_AUDITED, entities, cause);
    }

    /**
     * 停用
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void disable(String businessCode, List<? extends AuditModule> entities, String cause) {
        log(businessCode, AuditOperationEnum.DISABLE, entities, cause);
    }

    /**
     * 提交
     */
    private void log(String businessCode, AuditOperationEnum operation, List<? extends AuditModule> entities, String cause) {
        insertBatch(entities.stream()
                .map(v -> AuditLogEntity.builder()
                        .businessCode(businessCode)
                        .businessId(v.getId())
                        .operation(operation.getValue())
                        .status(operation.getStatus())
                        .cause(cause)
                        .build())
                .collect(Collectors.toList()));
    }

}
